//
//  WBMainViewController.swift
//  WBWeiBo
//
//  Created by 王斌 on 16/7/9.
//  Copyright © 2016年 王斌. All rights reserved.
//

import UIKit
import SVProgressHUD

class WBMainViewController: UITabBarController
{
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        // 添加子视图控制器的两种方式
        // viewControllers = [xx,]
        
        // addChildViewController(xx)
        
        //指定tabbar
        let wbTabbar = WBTabBar()
        
        //设置代理对象
        wbTabbar.wbDelegate = self
        
        //传入指定闭包
        wbTabbar.composeButtonClosure = { [weak self] in
            
            if !WBUserAccountViewModel.shareUserAccount.isLogin{
               SVProgressHUD.showErrorWithStatus("请先登录,亲")
                return
            }
            
            //  代码执行到此表示已经登录
            if let weakSelf = self{
                let composeView = WBComposeView()
                composeView.show(weakSelf)
            }
            
            
            //  获取最上面的一个window
//            let window = UIApplication.sharedApplication().windows.last!
//            
//            window.addSubview(composeView)
        }
        
        
        //使用kvc给只读属性设置值
        setValue(wbTabbar, forKey: "tabBar")
        
        
        addChildViewController(WBHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        
        addChildViewController(WBMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        
        addChildViewController(WBDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        
        addChildViewController(WBProfileTableViewController(), title: "我的", imageName: "tabbar_profile")
        
        
    }
    
    
    // 重载addChildViewControoler
    func addChildViewController(childController: UIViewController,title:String,imageName:String)
    {
        //设置tabbar的文字和显示图片
        childController.title = title
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")
   
    
        //指定渲染模式
        childController.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        //设置tabbar字体颜色
    childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: .Selected)
        
        
        //设置tabbar字体
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFontOfSize(13)], forState: .Normal)
        
        
        //添加子视图控制器
        let nav = UINavigationController(rootViewController: childController)
        
        addChildViewController(nav)
    
    
    }

}



extension WBMainViewController:WBTabBarDelegate
{
    func didSelectedComposeButton()
    {
        print("我是代理对象调用过来的")
    }
}

